package com.maila.xiaoaobusiness.activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;

import com.maila.xiaoaobusiness.R;
import com.maila.xiaoaobusiness.fragment.BusinessFragment;
import com.maila.xiaoaobusiness.fragment.OrderFragment;
import com.maila.xiaoaobusiness.fragment.PersonFragment;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabReselectListener;
import com.roughike.bottombar.OnTabSelectListener;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements OnTabSelectListener{

    @BindView(R.id.contentContainer)
    FrameLayout contentContainer;
    @BindView(R.id.bottomBar)
    BottomBar bottomBar;

    private OrderFragment orderFragment;
    private BusinessFragment businessFragment;
    private PersonFragment personFragment;
    FragmentManager fm;
    FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        fm= getFragmentManager();

        bottomBar.setOnTabSelectListener(this);
        bottomBar.setDefaultTab(R.id.tab_order);
    }


    @Override
    public void onTabSelected(int tabId) {
        transaction = fm.beginTransaction();
        //开启事务
        switch (tabId) {
            case R.id.tab_order:
                if (orderFragment == null) {
                    orderFragment =new  OrderFragment();
                }
                transaction.replace(R.id.contentContainer, orderFragment);
                break;
            case R.id.tab_business:
                if (businessFragment == null) {
                    businessFragment =new  BusinessFragment();
                }
                transaction.replace(R.id.contentContainer, businessFragment);
                break;
            case R.id.tab_personal:
                if (personFragment == null) {
                    personFragment = new PersonFragment();
                }
                transaction.replace(R.id.contentContainer, personFragment);
                break;
            default:
                break;
        }
        // 事务提交
        transaction.commit();

    }
}
